﻿;不显示托盘图标
;#NoTrayIcon  
#HotIf WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")

#HotIf
typoraPathKey := "TYPORA_PATH"
vscodePathKey := "VSCODE_PATH"
devecoStudioPathKey := "DEV_ECO_STUDIO_PATH"


pathMap := Map()
content := FileRead("config.json")
lines := StrSplit(content,"`r`n")
For index,line in lines {
    ;MsgBox index "-" line
    keyvalue := StrSplit(line,"=","`n")
    key := keyvalue[1]
    ;MsgBox key
    value := keyvalue[2]
    pathMap.Set(key,value)
}

getQuickOpenPath() {
    return pathMap.get("QuickOpenPath")
}
getTyporaPath() {
    return pathMap.get(typoraPathKey)
}
getVscodePath() {
    return pathMap.get(vscodePathKey)
}
getDevEcoStudioPath() {
    return pathMap.get(devecoStudioPathKey)
}


ExplorerGetSelection() {
    if !hwnd := WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")
        return
    try activeTab := ControlGetHwnd("ShellTabWindowClass1", hwnd)
    for w in ComObject("Shell.Application").Windows {
        if w.hwnd != hwnd
            continue
        if IsSet(activeTab) {
            static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
            shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
            ComCall(3, shellBrowser, "uint*", &thisTab:=0)
            if thisTab != activeTab
                continue
        }
        foundWin := w
        break
    }
    str := ""
    for i in foundWin.Document.SelectedItems
        str .= i.Path "`n"
    return str
}

getExplorerSelection2(){
    ;MsgBox "The active window's ID is " WinExist("A")
    processName := WinGetProcessName("A")
    className := WinGetClass("A")
    MsgBox "The active window is '" className "'."

}

getPathFromDesktop() {
    ;SEND "{AppsKey}"
    SEND "^+c"
    MsgBox(A_Clipboard)
    return A_Clipboard
}
getPathFromDesktop2(){
    shellWindows := ComObject("Shell.Application").Windows
    ;MsgBox winClass
      CLSID_ActiveDesktop := "{75048700-EF1F-11D0-9888-006097DEACF9}"
    IID_IActiveDesktop := "{F490EB00-1240-11D1-9888-006097DEACF9}"
    ;comObj := ComObject(CLSID_ActiveDesktop, IID_IActiveDesktop)
     comObj := ComValue(VT_UI4 := 0x13, SWC_DESKTOP := 0x8)
      ; MsgBox comObj
      shellFolderView := shellWindows.Item(comObj).Document
}

showComObjInfo(com) {
    MsgBox
(
    "Variant type:`t" ComObjType(com) "
    Interface name:`t" ComObjType(com, "Name") "
    Interface ID:`t" ComObjType(com, "IID") "
    Class name:`t" ComObjType(com, "Class") "
    Class ID (CLSID):`t" ComObjType(com, "CLSID")
)
}

#Requires AutoHotkey v2.0

;获取explorer中选中的文件夹的路径,目前对快捷方式无效.
ExplorerGetSelection2() {
   result := ""
   winClass := WinGetClass("ahk_id" . hWnd := WinExist("A"))
   if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
      Return
   
    shellWindows := ComObject("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
                                       {
    ;MsgBox winClass
      CLSID_ActiveDesktop := "{75048700-EF1F-11D0-9888-006097DEACF9}"
    IID_IActiveDesktop := "{F490EB00-1240-11D1-9888-006097DEACF9}"
    ;comObj := ComObject(CLSID_ActiveDesktop, IID_IActiveDesktop)
     comObj := ComValue(VT_UI4 := 0x13, SWC_DESKTOP := 0x8)
      ; MsgBox comObj
      shellFolderView := shellWindows.Item(comObj).Document
    }
   else {
      for window in shellWindows       ; ShellFolderView object: https://tinyurl.com/yh92uvpa
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   d := shellFolderView.SelectedItems
    ;showComObjInfo(d)
    item := shellFolderView.SelectedItems.Item(0)
    ;showComObjInfo(item)
   ;for item in shellFolderView.SelectedItems {}
    if(item.IsLink){
        ;是快捷方式,获取快捷方式的路径.
        result := item.GetLink.Path
    }
    else {
      result .= (result = "" ? "" : "`n") . item.Path
    }

   
   ;~ if !result
      ;~ result := shellFolderView.Folder.Self.Path
   Return result
}

;当菜单中有多个快捷键为A的就不好用了.
getExplorerSelection(){
    SEND "+{AppsKey}"
    SEND "A"
    filaPath := A_Clipboard
    MsgBox(filaPath)

}



;ctl+alt+t 用typora打开当前文件
^!t::
{
     path := ExplorerGetSelection2()
     if(path = "") {
     }
     else {
        appPath := getTyporaPath()
        cmd := appPath " " path
        Run cmd
     }
}

;ctl+d 用DevEcoSudio打开当前文件夹
^d::
{
     path := ExplorerGetSelection2()
     if(path = "") {
    
     }
     else {
        appPath := getDevEcoStudioPath()
        cmd := appPath " " path
        Run cmd
     }
}

;ctrl + v 用 vscode打开
^!v::
{
     path := ExplorerGetSelection2()
     if(path != "") {
       appPath := getVscodePath()
        cmd := appPath " " path
        Run cmd
     }
}

;;重新加载QuickOpen脚本.
^q:: {
    appPath := getQuickOpenPath()
        cmd := appPath
        Run appPath
}